package pikes.html.xhtml.list;

import java.util.LinkedList;
import java.util.List;

import pikes.html.xhtml.CoreAttributes;
import pikes.html.xhtml.Events;
import pikes.html.xhtml.Flow;
import pikes.html.xhtml.I18N;
import pikes.html.xhtml.NonRootTagFactory;
import pikes.html.xhtml.PCData;
import pikes.html.xhtml.TagDecorator;
import pikes.xml.XMLTag;

/**
 * <code>&lt;li/&gt;</code> tag, see <a href="http://www.w3.org/TR/html401/struct/lists.html#h-10.2">li element</a>
 * in HTML specification.
 * @author Peter Bona
 * @see <a href="http://www.w3.org/TR/html401/struct/lists.html#h-10.2">The li element in HTML specification</a>
 */
public class ListItem extends NonRootTagFactory implements TagDecorator, CoreAttributes, I18N, Events {

	private List<Flow> flows = new LinkedList<Flow>();

	/**
	 * @return "li"
	 */
	final protected CharSequence getTagName() {
		return "li";
	}

	/**
	 * Creates an empty list item
	 */
	public ListItem() {
		// left intentionally empty
	}

	/**
	 * Creates a list item with the first item of the given parameter Flow.
	 * @param flow the first item in the content
	 */
	public ListItem(Flow flow) {
		if (flow != null) {
			flows.add(flow);
		}
	}

	/**
	 * Creates a list item with the first item of the given string.
	 * @param text the first item in the content
	 */
	public ListItem(CharSequence text) {
		if (text != null) {
			flows.add(new PCData(text));
		}
	}

	/**
	 * Adds a flow item to the content of this list item.
	 * @param flow new item in the content
	 */
	final public void add(Flow flow) {
		if (flow != null) {
			flows.add(flow);
		}
	}

	/**
	 * Delegates the decoration of the parent tag to the added flow elements.
	 * @param tag The parent tag to be decorated.
	 */
	@Override
	final protected void decorateCreatedTag(XMLTag tag) {
		for (Flow flow : flows) {
			flow.decorateTag(tag);
		}
	}

}
